﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PhotoGallery.BLL;
using PhotoGallery.Model;
public partial class picupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] != null) 
        {
            User u = (User)Session["user"];
            if (Request["aid"]!=null)
            {
                AlbumAction aa = new AlbumAction();
                string aid = Request["aid"].ToString();
                Albums album = aa.showAlbum(aid);
                this.albumlink.Text = album.Title;
                this.albumlink.NavigateUrl = "albumshow.aspx?aid="+Request["aid"];
                this.modifyalbum.NavigateUrl = "album.aspx?aid=" + Request["aid"];
                if (Request["pid"] == null)
                {
                    //上传图片至该相册
                    this.iphoto.Visible = false;
                    this.modify.Visible = false;
                }
                else 
                {
                    //修改图片
                    this.iphoto.Visible = true;
                    this.uploadContainer.Visible = false;
                    this.modify.Visible = true;
                    this.h1.InnerText = "修改图片描述";
                    
                    PhotoAction pa = new PhotoAction();
                    string pid = Request["pid"].ToString();
                    Picture p=pa.showPic(pid);
                    if(!IsPostBack)
                        this.desp.Value = p.Desp;
                    Image1.ImageUrl = p.Pic;
                    Image1.Width=700;
                }
            }
            else 
            {
                if (Request["pid"] != null) 
                {
                    
                    this.iphoto.Visible = true;
                    this.uploadContainer.Visible = false;
                    this.modify.Visible = true;
                    this.h1.InnerText = "修改图片描述";

                    PhotoAction pa = new PhotoAction();
                    string pid = Request["pid"].ToString();
                    Picture p = pa.showPic(pid);
                    if (!IsPostBack)
                        this.desp.Value = p.Desp;
                    Image1.ImageUrl = p.Pic;
                    Image1.Width = 700;
                    AlbumAction aa = new AlbumAction();
                    string aid = p.Aid;
                    Albums album = aa.showAlbum(aid);
                    this.albumlink.Text = album.Title;
                    this.albumlink.NavigateUrl = "albumshow.aspx?aid=" + aid;
                    this.modifyalbum.NavigateUrl = "album.aspx?aid=" + aid;
                }
            }
        }
        else 
        { 
            Response.Redirect("login.aspx"); 
        }
    }
    protected void upload_Click(object sender, EventArgs e)
    {
        if (this.UploadFile.PostedFile != null)
        {
            string photoName1 = UploadFile.PostedFile.FileName; //获取初始文件名
            int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
            string newext = photoName1.Substring(i); //获取文件扩展名
            //判断文件是否为图片
            if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png"
                && newext != ".GIF" && newext != ".JPG" && newext != ".JPEG" && newext != ".BMP" && newext != ".PNG")
            {
                Response.Write("文件格式不正确!");
                Response.End();
            }
            string classid = ((User)Session["user"]).Uid;
            if (!Directory.Exists(HttpContext.Current.Server.MapPath("photos/") + "\\" + classid))
            {
                System.IO.Directory.CreateDirectory(@HttpContext.Current.Server.MapPath("photos/") + "\\" + classid);
            }
            //修改文件名
            photoName1 = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss-") + photoName1;
            UploadFile.PostedFile.SaveAs(Server.MapPath("photos\\" + classid + "\\" + photoName1));
            string path = "photos/" + classid + "/" + photoName1;
            AlbumAction aa = new AlbumAction();
            Albums a=aa.showAlbum(Request["aid"]);
            string pid = picUpload(photoName1, a,this.desp.Value, classid, path);
            if (pid != "0")//上传成功！
            {
                Response.Redirect("picupload.aspx?aid="+a.Aid+"&pid=" + pid);
            }

        }
    }
    //图像存入数据库,返回刚上传的图片的id
    private string picUpload(string filename, Albums album,string desp, string uid, string path)
    {
        //图像存入数据库
        PhotoAction pa = new PhotoAction();
        pa.uploadPictures(uid, filename,desp, album.Aid, path);
        Picture p = pa.getPicbyUidndFileName(uid, filename);

        if (p != null)
        {
            //取得刚上传图片的id
            return p.Id;
        }
        else
        {
            return "0";
        }
    }
    protected void modify_Click(object sender, EventArgs e)
    {
        PhotoAction pa = new PhotoAction();
        Picture p=pa.showPic(Request["pid"].ToString());
        p.Desp = this.desp.Value;
        pa.modifyPictures(p.Id, p);
    }
}